package refect;


import refect.domain.Person;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RefectDemo04Method {
    public static void main(String[] args)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {

        // 0. 获取Class对象
        Class psersonClass = Person.class;

        // 获取方法
        Method method = psersonClass.getMethod("eat");

        Person person = new Person();

        // 执行方法
        method.invoke(person);

        Method method1 = psersonClass.getMethod("eat", String.class);

        method1.invoke(person, "rice");

        System.out.println("----------------");

        Method[] methods = psersonClass.getMethods();
        for (Method method2 : methods) {
            System.out.println(method2);
        }

        System.out.println("----------------");

        Method[] methods1 = psersonClass.getDeclaredMethods();
        for (Method method2 : methods1) {
            // 暴力反射
            method2.setAccessible(true);

            System.out.println(method2.getName());
        }


    }

}
